import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error=> error)
}


const routes = [
  {
    path: '/user',
    component: () => import('../layouts/UserLayout.vue'),
    children: [
      {
        path: '/user',
        redirect: '/user/login'
      },
      {
        path: "/user/login",
        name: 'login',
        component: () => import('../views/user/login.vue')
      },


    ]
  },
  {
    path: "/",
    component: () => import("../layouts/BasicLayout.vue"),
    children: [
      {
        path: "/",
        redirect: "/statistics"
      },
      {
        path: '/statistics',
        name: 'Statistics',
        component: () => import('../views/dataStatistics/statistics.vue')
      },
      {
        path: '/management',
        name: 'Management',
        component: () => import('../views/userManagement/management.vue')
      },
      {
        path:'/region',
        name:'Region',
        component:() =>import('../views/region/region.vue')
      },
      {
        path:'/room',
        name:'Room',
        component:() =>import('../views/room/room.vue')
      },
      {
        path:'/alerts',
        name:'Alerts',
        component:() =>import('../views/alerts/alerts.vue')
      }
    ]
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


//路由控制守卫
router.beforeEach((to, from, next) =>{
  if(to.path === '/user/login')
  return next();
  const tokenStr = window.sessionStorage.getItem('this.data');
  if (!tokenStr) return next('/user/login');
  next();
})

export default router
